import { createMemoryHistory, createRouter } from 'vue-router'
import { useUserStore } from '~/store/user'


import FlowDeSign from '~/views/flow/design/index.vue'
import FlowCategory from '~/views/flow/category/index.vue'
import Table from '~/views/table/index.vue'
import TreeTable from '~/views/treeTable/index.vue'
import FlowConfig from '~/views/flowconfig/index.vue'
import FlowApproval from '~/views/flow/approval/index.vue'
import FlowTodo from '~/views/flow/todo/index.vue'
import FlowDone from '~/views/flow/done/index.vue'
import FlowMy from '~/views/flow/my/index.vue'
import Login from '~/views/login.vue'
import Layout from '~/components/layouts/index.vue';
import SysRole from '~/views/sys/role/index.vue'
import SysUser from '~/views/sys/user/index.vue'
import SysMenu from '~/views/sys/menu/index.vue'
import FormCreate from '~/views/design/formcreate/index.vue'
import Home from '~/views/home/index.vue'
const routes = [
  { path: '/', 
    name:"Layout" , 
    redirect: '/home',
    component: Layout,
    children:[
      { path: '/home', component: Home, meta:{title: "首页"} },
      { path: '/flow/definition', component: FlowDeSign , meta:{title: "流程设计"} },
      { path: '/table', component: Table , meta:{title: "表格页面"} },
      { path: '/treeTable', component: TreeTable , meta:{title: "树表页面"} },
      { path: '/flow/category', component: FlowCategory , meta:{title: "流程分类"} },
      { path: '/flow/flowconfig/:id', component: FlowConfig, name: "myconfig" , meta:{title: "流程配置"} },
      { path: '/flow/approval', component: FlowApproval, meta:{title: "发起流程"} },
      { path: '/flow/todo', component: FlowTodo, meta:{title: "我的待办"} },
      { path: '/flow/done', component: FlowDone, meta:{title: "我的已办"} },
      { path: '/flow/my', component: FlowMy,  meta:{title: "我发起的"}},
      { path: '/sys/role', component: SysRole, meta:{title: "角色管理"} },
      { path: '/sys/menu', component: SysMenu, meta:{title: "菜单管理"} },
      { path: '/sys/user', component: SysUser, meta:{title: "用户管理"} },
      { path: '/design/formcreate', component: FormCreate, meta:{title: "表单设计"} }
    ] 
  },
  { path: '/login', component: Login, name: "Login"  },
]

const router = createRouter({
  history: createMemoryHistory(),
  routes,
})
const  isLogin = true;
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  if (userStore.token || to.name == 'Login') {
    next()
  }
  else {
    next({ name: "Login" })
  }
})

export default router;